package com.shiwaixiangcun.monitor.security;

import com.shiwaixiangcun.core.utils.WebUtils;
import com.shiwaixiangcun.core.web.response.ResponseCode;
import com.shiwaixiangcun.core.web.response.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MsAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
    /**
     * @param loginFormUrl URL where the login page can be found. Should either be
     *                     relative to the web-app context path (include a leading {@code /}) or an absolute
     *                     URL.
     */
    public MsAuthenticationEntryPoint(String loginFormUrl) {
        super(loginFormUrl);
    }


    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        if (WebUtils.isAjaxRequest(request)) {
            ResponseEntity responseEntity = new ResponseEntity();
            responseEntity.fail(ResponseCode.NOT_LOGIN, "还未登录请先登录");
            WebUtils.writeJSON(response, responseEntity);
            return;
        }

        super.commence(request, response, authException);
    }
}
